package com.weather.service;

import com.weather.common.utils.XmlBuilder;
import com.weather.domain.City;
import com.weather.domain.CityList;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

/**
 * @author: htc
 * @date: Created in 17:03 2018/5/4.
 */
@Service
public class CityDataServiceImpl implements CityDataService {
    @Override
    public List<City> listCity() throws Exception {
        //读取XML文件
        Resource resource = new ClassPathResource("cityList.xml");
        BufferedReader bf = new BufferedReader(new InputStreamReader(resource.getInputStream(), "UTF-8"));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while ((line = bf.readLine()) != null) {
            buffer.append(line);
        }
        bf.close();
        //XML转为java对象
        CityList cityList = (CityList) XmlBuilder.xml2Obj(CityList.class, buffer.toString());
        return cityList.getCityList();
    }
}
